package 其他.计算单词出现的次数并排序输出;


import java.util.*;

public class Main {
    private static Map<String,Integer> map;

    public static void main(String[] args) {
        String s="New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3";
        solution(s);
    }
    public static void solution(String str){
        if (str==null){
            return;
        }
        if (str.length()==0){
            return;
        }

        String[] s = str.split(" ");
        map=new HashMap<>();
        for (int i = 0; i < s.length; i++) {

            if (map.containsKey(s[i])){
                Integer num = map.get(s[i]);
                map.put(s[i],++num);
            }else {
                map.put(s[i],1);
            }
        }

        TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int diff = map.get(o2) - map.get(o1);
                return diff == 0 ? (o1.compareTo(o2)) : diff;
            }
        });
        Set<String> set = map.keySet();
        for (String s1 : set) {
            treeSet.add(s1);
        }

        for (String key : treeSet) {
            System.out.println(key+":"+map.get(key));
        }
    }

}
